package com.youlai.boot.food.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.food.mapper.SysFoodInfoMapper;
import com.youlai.boot.food.service.SysFoodInfoService;
import com.youlai.boot.food.model.entity.SysFoodInfo;
import com.youlai.boot.food.model.form.SysFoodInfoForm;
import com.youlai.boot.food.model.query.SysFoodInfoQuery;
import com.youlai.boot.food.model.vo.SysFoodInfoVO;
import com.youlai.boot.food.converter.SysFoodInfoConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 食物数据服务实现类
 *
 * @author cuisheng
 * @since 2025-03-30 16:17
 */
@Service
@RequiredArgsConstructor
public class SysFoodInfoServiceImpl extends ServiceImpl<SysFoodInfoMapper, SysFoodInfo> implements SysFoodInfoService {

    private final SysFoodInfoConverter sysFoodInfoConverter;

    /**
    * 获取食物数据分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysFoodInfoVO>} 食物数据分页列表
    */
    @Override
    public IPage<SysFoodInfoVO> getSysFoodInfoPage(SysFoodInfoQuery queryParams) {
        Page<SysFoodInfoVO> pageVO = this.baseMapper.getSysFoodInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取食物数据表单数据
     *
     * @param id 食物数据ID
     * @return
     */
    @Override
    public SysFoodInfoForm getSysFoodInfoFormData(Long id) {
        SysFoodInfo entity = this.getById(id);
        return sysFoodInfoConverter.toForm(entity);
    }
    
    /**
     * 新增食物数据
     *
     * @param formData 食物数据表单对象
     * @return
     */
    @Override
    public boolean saveSysFoodInfo(SysFoodInfoForm formData) {
        SysFoodInfo entity = sysFoodInfoConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新食物数据
     *
     * @param id   食物数据ID
     * @param formData 食物数据表单对象
     * @return
     */
    @Override
    public boolean updateSysFoodInfo(Long id,SysFoodInfoForm formData) {
        SysFoodInfo entity = sysFoodInfoConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除食物数据
     *
     * @param ids 食物数据ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysFoodInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的食物数据数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
